import 'package:flutter/material.dart';
import 'dart:async';
import 'package:webview_flutter/webview_flutter.dart';

// ignore: must_be_immutable
class WebViewPage extends StatefulWidget {
  String title;
  String id;
  String url;

  WebViewPage({this.title, this.url, this.id});

  @override
  State<StatefulWidget> createState() {
    return WebViewState();
  }
}

class WebViewState extends State<WebViewPage> {
  final Completer<WebViewController> _controller = Completer<WebViewController>();
  IconData iconData = Icons.favorite_border;
  Color color = Colors.white;
  bool isFavorite = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Color(0xff313131),
        title: Text(
          widget.title,
          textAlign: TextAlign.center,
          style: TextStyle(color: Colors.white),
        ),
        actions: <Widget>[
          IconButton(
            icon: Icon(iconData, color: color),
            onPressed: () {
              favorite();
            },
          ),
          IconButton(
            icon: Icon(Icons.mode_edit, color: Colors.white),
            onPressed: () {

            }
    )
        ],
      ),
      body: WebView(
        initialUrl: widget.url,
        javascriptMode: JavascriptMode.unrestricted,
        onWebViewCreated: (controller) {
          _controller.complete(controller);
        },
      ),
    );
  }

  // 收藏
  void favorite() {
    setState(() {
      isFavorite = !isFavorite;
      if(isFavorite) {
        iconData = Icons.favorite;
        color = Color(0xffAC8B51);
      } else {
        iconData = Icons.favorite_border;
        color = Colors.white;
      }
    });
  }

}
